-- file: ch06/naiveeq.hs
{-# LANGUAGE BinaryLiterals #-}

import EqClasses

-- Color 的相等 == 测试
data Color = Red | Green | Blue

colorEq :: Color -> Color -> Bool
colorEq Red Red = True
colorEq Green Green = True
colorEq Blue Blue = True
colorEq _ _ = False

-- 字符串的相等测试
stringEq :: [Char] -> [Char] -> Bool
-- Match if both are empty
stringEq [] [] = True
-- If both start with the same char, check the rest
stringEq (x : xs) (y : ys) = x == y && stringEq xs ys
-- Everything else doesn't match
stringEq _ _ = False

--
instance BasicEq3 Color where
  isEqual3 Red Red = True
  isEqual3 Blue Blue = True
  isEqual3 Green Green = True
  isEqual3 _ _ = False

instance Show Color where
  show Red = "Color 1: Red"
  show Green = "Color 2: Green"
  show Blue = "Color 3: Blue"

-- file: ch06/naiveeq.hs
instance Read Color where
  -- readsPrec is the main function for parsing input
  readsPrec _ value =
    -- We pass tryParse a list of pairs.  Each pair has a string
    -- and the desired return value.  tryParse will try to match
    -- the input to one of these strings.
    tryParse [("Red", Red), ("Green", Green), ("Blue", Blue)]
    where
      tryParse [] = [] -- If there is nothing left to try, fail
      tryParse ((attempt, result) : xs) =
        -- Compare the start of the string to be parsed to the
        -- text we are looking for.
        if (take (length attempt) value) == attempt
          -- If we have a match, return the result and the
          -- remaining input
          then [(result, drop (length attempt) value)]
          -- If we don't have a match, try the next pair
          -- in the list of attempts.
          else tryParse xs

-- --------------
data A = A {foo :: Int, bar :: String} deriving (Show)

defaultA :: A
defaultA = A {foo = 0, bar = ""}

doSomething :: Bool -> A
doSomething True = defaultA {foo = 32}
doSomething False = defaultA {bar = "hello!"}